package com.gxa.controller;

import com.github.pagehelper.PageHelper;
import com.gxa.common.uitls.R;
import com.gxa.dto.*;
import com.gxa.entity.Apply;
import com.gxa.entity.ApplyGoods;
import com.gxa.entity.Emp;
import com.gxa.service.ApplyService;
import com.gxa.service.GoodsDetailService;
import com.gxa.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Api(tags = "请购单接口")
@RestController
public class ApplyController {

    @Autowired
    private ApplyService applyService;

    @Autowired
    private GoodsDetailService  goodsDetailService;

    @Autowired
    private GoodsService goodsService;


    @ApiOperation("点击请购单，请购单页面数据展示,与搜索使用一个接口")
    @PostMapping("/apply/applylist")
    public R queryAll(@RequestBody ApplyDto applyDto) {

        try {
            PageHelper.startPage(applyDto.getPage(),applyDto.getLimit());  //使用此方法进行分页

            List<Apply> applies = this.applyService.queryAll(applyDto);

            Map<String,Object> map =new HashMap<>();
            map.put("applies",applies);

            return R.ok(map);

        }catch (Exception e){
            e.printStackTrace();
           return R.error();
        }

    }

    @PostMapping("/apply/applyAdd")
    @ApiOperation("新增请购单的保存")
    public R applyGoodsAdd(@RequestBody ApplyAddDto applyAddDto) {

        try {

        String purchaseNo = this.createPurchaseNo();
        applyAddDto.setApplyno(purchaseNo);

        this.applyService.addApply(applyAddDto);

        return R.ok("添加成功");

        }catch (Exception e){
            e.printStackTrace();

            return R.error();
        }


    }

    @ApiOperation("查询人员,和查询部门")
    @PostMapping("/apply/emp")
    public R queryEmp(@RequestBody EmpDto empDto){

//      PageHelper.startPage(page, limit);  //使用此方法进行分页

        try {

            List<Emp> emps = this.applyService.queryEmp(empDto);

            Map<String,Object> map =new HashMap<>();
            map.put("emps",emps);

            return  R.ok(map);

        }catch (Exception e){
            e.printStackTrace();

            return R.error();
        }

    }


    @GetMapping("/apply/selgoods")
    @ApiOperation("根据请购单id查询商品")
    public R applyAdd(Integer id){

        try {
            List<GoodsDetailsDto> goods = this.goodsDetailService.selGoods(id);
            Map<String,Object> map =new HashMap<>();
            map.put("goods",goods);

            return R.ok(map);

        }catch (Exception e){
            e.printStackTrace();

            return R.error();
        }

    }

    @GetMapping("/apply/goods")
    @ApiOperation("查询商品")
    public R queryGoods(){

        try {

            List<GoodsQueryDto> goodsList = this.goodsService.queryGoodsList();
            Map<String,Object> map =new HashMap<>();
            map.put("goodsList",goodsList);

            return R.ok(map);

        }catch (Exception e){
            e.printStackTrace();

            return R.error();
        }

    }


    @ApiOperation("请购单编辑保存")
    @PostMapping("/apply/update")
    public R applyupdate(@RequestBody ApplyAddDto applyAddDto) {

        try {

            this.applyService.updateApply(applyAddDto);

            return R.ok();

        }catch (Exception e){
            e.printStackTrace();

            return R.error();
        }

    }


    @ApiOperation("指派员工保存，返回当前请购单编号和指派的员工姓名")
    @PostMapping("/apply/edit")
//    public R updateEmp(@ApiParam("请购单编号") String applyno, @ApiParam("指派的员工id") Integer eid) {
    public R updateEmp(@ApiParam("请购单编号") String applyno, @ApiParam("指派的员工id") Integer eid) {

        try {

            this.applyService.updateAssign(applyno, eid);

            return R.ok();

        }catch (Exception e){
            e.printStackTrace();

            return R.error();
        }
    }

        @ApiOperation("撤销接口，需要给到请购单编号")
        @PostMapping("/apply/delete")
        public R DeleteEmp(@ApiParam("请购单编号") String applyno) {

            try {

                this.applyService.updateBackout(applyno);

                return R.ok();

            }catch (Exception e){
                e.printStackTrace();

                return R.error();
            }
        }


    @ApiOperation("审核通过确定，返回当前请购单编号和当前用户id，1:审核未通过，2：审核通过，3：待分配，不通过请添加说明remark")
    @PostMapping("/apply/check")
    public R updateCheck(@RequestBody ApplyAddDto applyAddDto) {

        try {

            this.applyService.updateCheck(applyAddDto);

            return R.ok();

        }catch (Exception e){
            e.printStackTrace();

            return R.error();
        }
    }

    @ApiOperation("删除接口")
    @PostMapping("/apply/del")
//    private R deleteApply(@RequestBody ApplyAddDto applyAddDto){
    private R deleteApply(String[] ids){

        try {

//            this.applyService.deleteApply(applyAddDto);
            this.applyService.deleteApply(ids);

            return R.ok();

        }catch (Exception e){
            e.printStackTrace();

            return R.error();
        }

    }

    //构建请购单编号
    private String createPurchaseNo(){
        StringBuilder sb = new StringBuilder();

        //获取当前日期
        Date date = new Date(System.currentTimeMillis());
        int year = date.getYear();
        int month = date.getMonth();
        int day = date.getDay();
        sb.append("QG" + year + month + day);

        Random random = new Random();
        int ends = random.nextInt(99);
        String format = String.format("%02d", ends);//如果不足两位，前面补0
        sb.append(format);

        return sb.toString();
    }

}
